<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>工人管理系统 - 登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #3498db, #2c3e50);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        .auth-container {
            width: 100%;
            max-width: 400px;
        }

        .auth-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
            padding: 30px;
        }

        .logo {
            text-align: center;
            margin-bottom: 25px;
        }

        .logo i {
            font-size: 3rem;
            color: #3498db;
            margin-bottom: 10px;
        }

        .logo h1 {
            color: #2c3e50;
            font-size: 1.8rem;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #2c3e50;
        }

        input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 6px;
            font-size: 1rem;
            transition: all 0.3s;
        }

        input:focus {
            border-color: #3498db;
            outline: none;
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
        }

        .btn {
            width: 100%;
            padding: 12px;
            border: none;
            border-radius: 6px;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 8px;
        }

        .btn-primary {
            background: #3498db;
            color: white;
        }

        .btn-primary:hover {
            background: #2980b9;
        }

        .btn-success {
            background: #2ecc71;
            color: white;
        }

        .btn-success:hover {
            background: #27ae60;
        }

        .error-message {
            background: #f8d7da;
            color: #721c24;
            padding: 10px;
            border-radius: 4px;
            margin-bottom: 15px;
            text-align: center;
            display: none;
        }

        .auth-footer {
            text-align: center;
            margin-top: 20px;
            color: #7f8c8d;
        }

        .auth-footer a {
            color: #3498db;
            text-decoration: none;
            font-weight: 600;
        }

        .auth-footer a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="auth-container">
    <div class="auth-card">
        <div class="logo">
            <h1>工人管理系统</h1>
        </div>

        <div class="error-message" id="errorMessage">
            <!-- 错误信息将在这里显示 -->
        </div>

        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" placeholder="请输入用户名" required>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码" required>
            </div>

            <button type="submit" class="btn btn-primary">登录</button>
        </form>

        <div class="auth-footer">
            <p>没有账号？<a href="register.html">立即注册</a></p>
        </div>
    </div>
</div>

<script>
    // 等待DOM完全加载后再执行
    document.addEventListener('DOMContentLoaded', function() {
        const loginForm = document.getElementById('loginForm');
        const messageDiv = document.getElementById('errorMessage');

        if (!loginForm) {
            console.error('登录表单未找到！');
            return;
        }

        loginForm.addEventListener('submit', async function(e) {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            try {
                // 显示加载状态
                messageDiv.textContent = '登录中...';
                messageDiv.style.display = 'block';
                messageDiv.style.background = '#d1ecf1';
                messageDiv.style.color = '#0c5460';

                const response = await fetch('/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });

                // 首先检查响应内容类型
                const contentType = response.headers.get('content-type');

                if (contentType && contentType.includes('application/json')) {
                    const result = await response.json();

                    if (result.success) {
                        messageDiv.textContent = '登录成功，正在跳转...';
                        messageDiv.style.background = '#d4edda';
                        messageDiv.style.color = '#155724';

                        // 跳转到管理页面
                        setTimeout(() => {
                            window.location.href = '/index.html';
                        }, 1000);
                    } else {
                        throw new Error(result.message);
                    }
                } else {
                    // 如果不是JSON响应，读取文本内容
                    const text = await response.text();
                    console.error('非JSON响应:', text.substring(0, 100));
                    throw new Error('服务器返回了错误的响应格式');
                }

            } catch (error) {
                console.error('登录错误:', error);
                messageDiv.textContent = '登录失败: ' + error.message;
                messageDiv.style.background = '#f8d7da';
                messageDiv.style.color = '#721c24';
                messageDiv.style.display = 'block';
            }
        });
    });
</script>
</body>
</html>